(function( _ , $ ){
	_.Html.contextmenu = "<div class='jdmer_comtextmenu'>"+
	"<div class='cm_bg'>"+
	"	<div class='cm_top'>"+
	"		<div class='cm_left'></div>"+
	"		<div class='cm_center'></div>"+
	"		<div class='cm_right'></div>"+
	"	</div>"+
	"	<div class='cm_middle'>"+
	"		<div class='cm_left'></div>"+
	"		<div class='cm_center'></div>"+
	"		<div class='cm_right'></div>"+
	"	</div>"+
	"	<div class='cm_bottom'>"+
	"		<div class='cm_left'></div>"+
	"		<div class='cm_center'></div>"+
	"		<div class='cm_right'></div>"+
	"	</div>"+
	"</div>"+
	"<div class='cm_content'>"+
	"</div>"+
	"</div>";
	
	var Contextmenu = function(_config){
		var _this = this;
		this.uid = _.Util.guid();	//唯一编号
		var config = {
			menuArray : new Array(),
			menuMap : new _.Map(),
			childrenMap : new _.Map(),
			top : 0,
			left : 0 ,
			height : 200,
			width : 200,
			container : "body"
		};
		$.extend(config,_config);	//合并用户的配置参数
		this.jQuery = null;
		var jq = {
			obj : null,
			bg:null,
			top:null,
			top_left : null,
			top_center : null,
			top_right : null,
			middle:null,
			middle_left : null,
			middle_center : null,
			middle_right : null,
			bottom:null,
			bottom_left : null,
			bottom_center : null,
			bottom_right : null,
			content:null
		};
		var htmlBase = _.Html.contextmenu;	//html原型
		
		/**
		 * 显示出来
		 */
		this.display = function(){
			if(jq.obj){
				return this;
			}
			initjQuery();
			jq.obj.attr("jdmer_uid",_this.uid);
			_this.container(config.container);
			_this.top(config.top);
			_this.left(config.left);
			if(config.menuArray){
				_this.byMenuArray(config.menuArray);
				config.menuArray = undefined;
			}else{
				_this.byMenuMap(config.menuMap);
			}
			jq.obj.data("jDreamerObject",_this);
			return this;
		};
		
		/**
		 * 消失不见
		 */
		this.vanish = function(){
			if(jq.obj){
				jq.obj.remove();
				clearjQuery();
			}
		};
		
		/**
		 * 销毁当前对象
		 */
		this.destroy = function(){
			_this.vanish();
			_this = undefined;
			delete _this;
		};
		
		/**
		 * 适应宽度
		 */
		this.fitWidth = function(){
			if(jq.obj){
				var allMenu = jq.content.children(".menu_item");
				var perWidth = 0;
				allMenu.each(function(i){
					var menu = allMenu.eq(i);
					var mL = menu.children(".mi_left");
					var mC = menu.children(".mi_center");
					var mR = menu.children(".mi_right");
					mC.width(1);
					var w = mC.get(0).scrollWidth + mL.width() + mR.width() + 20;
					if(w > perWidth){
						perWidth = w;
					}
				});
				_this.width(perWidth);
			}
			return this;
		};
		
		this.width = function(_width){
			if(_width == undefined){
				return config.width;
			}else{
				config.width = _width;
				if(jq.obj){
					jq.obj.width(config.width);
					jq.bg.width(config.width);
					jq.top_center.width(config.width - jq.top_left.width() - jq.top_right.width());
					var centerWidth = config.width - jq.middle_left.width() - jq.middle_right.width();
					jq.middle_center.width(centerWidth);
					
					var contentWidth = config.width - jq.content.box().marginX;
					jq.content.width(contentWidth);
					
					jq.bottom_center.width(config.width - jq.bottom_left.width() - jq.bottom_right.width());
					
					var allMenu = jq.content.children(".menu_item");
					allMenu.each(function(i){
						var m = allMenu.eq(i);
						var mL = m.children(".mi_left");
						var mC = m.children(".mi_center");
						var mR = m.children(".mi_right");
						m.width(contentWidth);
						mC.width(contentWidth - mL.width() - mR.width());
					});
				}
				return this;
			}
		};
		
		/**
		 * 根据Map生成菜单
		 */
		this.byMenuMap = function(_menuMap){
			_menuMap.call(function(menu){
				_this.addMenu(menu);
			});
			_this.fitHeight();
			_this.fitWidth();
			return this;
		};
		
		/**
		 * 根据array生成菜单
		 */
		this.byMenuArray = function(_menuArray){
			for(i in _menuArray){
				var menu = _menuArray[i];
				_this.addMenu(menu);
			}
			_this.fitHeight();
			_this.fitWidth();
			return this;
		};
		
		/**
		 * 添加菜单
		 */
		this.addMenu = function(_menu){
			var menu = {	//默认属性
				id : _.Util.guid(),	//唯一编号
				mark : false,	//是否作为一个分隔符显示
				icon : null,	//图标地址
				iconClass : null,	//可以在一个class中获取图标
				label : "菜单项",	//菜单显示的文字
				disable : false,	//是否不可用
				callback : function(){},	//回调函数
				child : []	//子菜单
			};
			$.extend(menu,_menu);	//何必属性
			config.menuMap.put(menu.id,menu);	//放入Map中
			_this.mark(menu.id,menu.mark);
			_this.label(menu.id,menu.label);
			_this.icon(menu.id,menu.icon);
			_this.iconClass(menu.id,menu.iconClass);
			_this.disable(menu.id,menu.disable);
			_this.callback(menu.id,menu.callback);
			_this.child(menu.id,menu.child);
		};
		
		/**
		 * 子元素
		 */
		this.child = function(_id,_child){
			var menu = config.menuMap.get(_id);
			if(menu){
				if(menu.mark){
					return this;
				}
				if(_child == undefined){
					return menu.child;
				}else{
					menu.child = _child;
					menu.childContextmenu = _.contextmenu({	//创建一个子对象
						menuArray : menu.child
					});
					config.childrenMap.put(menu.id,menu.childContextmenu);
					config.menuMap.put(_id,menu);
					return this;
				}
			}
			return this;
		};
		
		/**
		 * 回调函数
		 */
		this.callback = function(_id,_callback){
			var menu = config.menuMap.get(_id);
			if(menu){
				if(menu.mark){
					return this;
				}
				if(_callback == undefined){
					return menu.callback;
				}else{
					menu.callback = _callback;
					config.menuMap.put(_id,menu);
					return this;
				}
			}
			return this;
		};
		
		/**
		 * 不可用
		 */
		this.disable = function(_id,_disable){
			var menu = config.menuMap.get(_id);
			if(menu){
				if(menu.mark){
					return this;
				}
				if(_disable == undefined){
					return menu.disable;
				}else{
					menu.disable = _disable;
					config.menuMap.put(_id,menu);
					if(jq.obj){
						var jqMenu = jq.content.children("[id='"+_id+"']");
						if(menu.disable){
							jqMenu.addClass("menu_item_disable");
						}else{
							jqMenu.removeClass("menu_item_disable");
						}
					}
					return this;
				}
			}
			return this;
		};
		
		/**
		 * 设置图标
		 */
		this.iconClass = function(_id,_iconClass){
			var menu = config.menuMap.get(_id);
			if(menu){
				if(menu.mark){
					return this;
				}
				if(_iconClass == undefined){
					return menu.iconClass;
				}else{
					menu.iconClass = _iconClass;
					config.menuMap.put(_id,menu);
					if(jq.obj){
						var img = jq.obj.getImageFromClass(menu.iconClass);
						_this.icon(_id,img);
					}
					return this;
				}
			}
			return this;
		};
		
		/**
		 * 设置图标
		 */
		this.icon = function(_id , _icon){
			var menu = config.menuMap.get(_id);
			if(menu){
				if(menu.mark){
					return this;
				}
				if(_icon == undefined){
					return menu.icon;
				}else{
					menu.icon = _icon;
					config.menuMap.put(_id,menu);
					if(jq.obj){
						var jqMenu = jq.content.children("[id='"+_id+"']");
						var img = jqMenu.find("img");
						img.show();
						img.attr("src",menu.icon);
					}
					return this;
				}
			}
			return this;
		};
		
		this.mark = function(_id , _mark){
			var menu = config.menuMap.get(_id);
			if(menu){
				if(_mark == undefined){
					return menu.mark;
				}else{
					menu.mark = _mark;
					config.menuMap.put(_id,menu);
					if(jq.obj){
						var jqMenu = jq.content.children("[id='"+_id+"']");
						jqMenu.remove();
						if(_mark){	//如何是分隔符
							jqMenu = $("<div id='"+menu.id+"' class='menu_mark'></div>");
							jqMenu.appendTo(jq.content);
						}else{	//如果是菜单
							jqMenu = $("<div id='"+menu.id+"' class='menu_item'>"+
								"<div class='mi_left'>"+
								"	<img style='display:none;'/>"+
								"</div>"+
								"<div class='mi_center'></div>"+
								"<div class='mi_right'></div>"+
								"</div>");
							
							var t = 0;
							var l = 0;
							var w = 0;
							var ot = 0;
							var cmW = 0;
							var cmH = 0;
							
							jqMenu.appendTo(jq.content);
							jqMenu.mouseenter(function(){
								menu = config.menuMap.get(_id);
								if(!menu.disable){
									jqMenu.addClass("menu_item_hover");
								}
								config.childrenMap.call(function(val,key){
									val.vanish();
								});
								if(menu.child.length > 0){
									t = _this.top();
									l = _this.left();
									w = jqMenu.width();
									ot = jqMenu.get(0).offsetTop;
									
									menu.childContextmenu.top(t + ot).left(l + w).display();
									cmW = menu.childContextmenu.width();
									cmH = menu.childContextmenu.height();
								}
							}).mouseover(function(){
								if(!menu.disable){
									jqMenu.addClass("menu_item_hover");
								}
							}).mouseout(function(e){
								menu = config.menuMap.get(_id);
								if(!menu.disable){
									jqMenu.removeClass("menu_item_hover");
								}
							});
							jqMenu.click(function(){
								menu = config.menuMap.get(_id);
								if(menu.disable || menu.child.length > 0){
									return false;
								}
								menu.callback(jqMenu,_this);
							});
						}
					}
					return this;
				}
			}
			return this;
		};
		
		/**
		 * 设置或者返回label
		 */
		this.label = function(_id,_label){
			var menu = config.menuMap.get(_id);
			if(menu){
				if(menu.mark){
					return this;
				}
				if(_label == undefined){
					return menu.label;
				}else{
					menu.label = _label;
					config.menuMap.put(_id,menu);
					if(jq.obj){
						var jqMenu = jq.content.children("[id='"+_id+"']");
						jqMenu.children(".mi_center").html(menu.label);
					}
					return this;
				}
			}
			return this;
		};
		
		/**
		 * 设置高度
		 */
		this.height = function(_height){
			if(_height == undefined){
				return config.height;
			}else{
				config.height = _height;
				if(jq.obj){
					jq.obj.height(config.height);
					jq.bg.height(config.height);
					var middleHeight = config.height - jq.top.height() - jq.bottom.height();
					jq.content.height(middleHeight);
					jq.middle.height(middleHeight);
					jq.middle_left.height(middleHeight);
					jq.middle_center.height(middleHeight);
					jq.middle_right.height(middleHeight);
				}
				return this;
			}
		};
		
		/**
		 * 自动适应高度
		 */
		this.fitHeight = function(){
			if(jq.obj){
				var allHeight = 0;
				allHeight = allHeight + jq.top.height() + jq.bottom.height();
				var allMenu = jq.content.children(".menu_item");
				var menuSize = allMenu.size();
				if(menuSize > 0){
					allHeight = allHeight + menuSize * allMenu.eq(0).height();
				}
				var allMark = jq.content.children(".menu_mark");
				var markSize = allMark.size();
				if(markSize > 0){
					allHeight = allHeight + markSize * allMark.eq(0).height();
				}
				_this.height(allHeight);
			}
			return this;
		};
		
		/**
		 * 设置或返回top值
		 */
		this.top = function(_top){
			if(_top == undefined){
				return config.top;
			}else{
				config.top = _top;
				if(jq.obj){
					jq.obj.css("top",config.top);
				}
				return this;
			}
		};
		
		/**
		 * 设置或返回left值
		 */
		this.left = function(_left){
			if(_left == undefined){
				return config.left;
			}else{
				config.left = _left;
				if(jq.obj){
					jq.obj.css("left",config.left);
				}
				return this;
			}
		};
		
		/**
		 * 设置容器
		 * @param _container	容器
		 */
		this.container = function(_container){
			if(_container == undefined){
				return $(config.container);
			}else{
				config.container = _container;
				if(jq.obj){
					jq.obj.appendTo($(config.container));
				}
				return this;
			}
		};
		
		/**
		 * 初始化jQuery对象
		 * @returns
		 */
		var initjQuery = function(){
			jq.obj = $(htmlBase);
			this.jQuery = jq.obj;
			jq.bg = jq.obj.children(".cm_bg");
			jq.top = jq.bg.children(".cm_top");
			jq.top_left = jq.top.children(".cm_left");
			jq.top_center = jq.top.children(".cm_center");
			jq.top_right = jq.top.children(".cm_right");
			jq.middle = jq.bg.children(".cm_middle");
			jq.middle_left = jq.middle.children(".cm_left");
			jq.middle_center = jq.middle.children(".cm_center");
			jq.middle_right = jq.middle.children(".cm_right");
			jq.bottom = jq.bg.children(".cm_bottom");
			jq.bottom_left = jq.bottom.children(".cm_left");
			jq.bottom_center = jq.bottom.children(".cm_center");
			jq.bottom_right = jq.bottom.children(".cm_right");
			jq.content = jq.obj.children(".cm_content");
		};
		

		/**
		 * 初始化jQuery对象
		 * @returns
		 */
		var clearjQuery = function(){
			this.jQuery = null;
			jq.obj = null;
			jq.bg = null;
			jq.top = null;
			jq.top_left = null;
			jq.top_center = null;
			jq.top_right = null;
			jq.middle = null;
			jq.middle_left = null;
			jq.middle_center = null;
			jq.middle_right = null;
			jq.bottom = null;
			jq.bottom_left = null;
			jq.bottom_center = null;
			jq.bottom_right = null;
			jq.content = null;
		};
	};
	_.Contextmenu = Contextmenu;
	_.contextmenu = function(_config){
		return new _.Contextmenu(_config);
	};
})(jDreamer,jQuery);